package com.zls.controller;

import com.github.pagehelper.PageInfo;
import com.zls.pojo.Department;
import com.zls.pojo.Employee;
import com.zls.service.DepartmentService;
import com.zls.service.EmployeeService;
import com.zls.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(path = "/admin/empl/")
public class EmpController {
    @Autowired
    private EmployeeService service;
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping(path = "list/{pageindex}/{pagesize}")
    @ResponseBody
    public JsonResult getEmps(@PathVariable(name = "pageindex") Integer pageindex, @PathVariable(name = "pagesize") Integer pagesize){
        PageInfo<Employee> emps = service.getEmpByPage(pageindex, pagesize);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setData(emps);
        return jsonResult;
    }
    @RequestMapping(path = "list")
    public String toList(Model model) {
        List<Employee> all = service.getAll();
        model.addAttribute("empls", all);
        return "empl_list";
    }
    @RequestMapping(path = "listjson")
    @ResponseBody
    public List<Employee> toListJson(Model model) {
        List<Employee> all = service.getAll();
        return all;
    }

    @RequestMapping(path = "toadd")
    public String toAdd(Model model) {
        List<Department> depts = departmentService.getAll();
        model.addAttribute("depts", depts);
        return "empl_toadd";
    }

    @RequestMapping(path = "doadd")
    @ResponseBody
    public JsonResult doAdd(HttpServletResponse resp, Employee empl) {
        JsonResult jsonResult=new JsonResult();
        int result = service.addEmp(empl);
        if (result > 0) {
            jsonResult.setData(result);
        } else if (result == -1) {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("添加失败");
        }
        return jsonResult;
    }

    @GetMapping(path = "dodel/{empid}")
    @ResponseBody
    public JsonResult doDel(HttpServletResponse resp, @PathVariable(name = "empid") String id) {
        int i = service.dropEmpById(Integer.parseInt(id));
        JsonResult jsonResult=new JsonResult();
        if (i > 0) {
            jsonResult.setData(i);
            jsonResult.setErrorCode(200);
        } else if (i == -1) {
            jsonResult.setErrorMsg("删除失败");
            jsonResult.setErrorCode(500);
        }
        return jsonResult;
    }
    @RequestMapping(path = "toedit/{empid}")
    @ResponseBody
    public JsonResult toEdit(@PathVariable(name = "empid") String id) {
        JsonResult jsonResult=new JsonResult();
        Employee empById = service.getEmpById(Integer.parseInt(id));
        jsonResult.setData(empById);
        return jsonResult;
    }
    @RequestMapping(path = "doedit")
    @ResponseBody
    public JsonResult doEdit(Employee empl) {
        JsonResult jsonResult=new JsonResult();
        int result = service.editEmp(empl);
        if (result > 0) {
            jsonResult.setData(result);
        } else if (result == -1) {
            jsonResult.setErrorCode(500);
            jsonResult.setErrorMsg("修改失败");
        }
        return jsonResult;
    }
}
